#include <iostream>

using namespace std;

#define BASE 26 // 26 lowercase letters

typedef struct Node{
    struct Node* next[26];  // a node can has 26 edges
    int flag;  // it is a word
} Node;

Node* getNewNode(){
    Node* p = (Node*)malloc(sizeof(Node));
    for(int i = 0; i < BASE; ++i) p->next[i] = NULL;
    p->flag = 0;
    return p;
}

void clear(Node* root){
    if(root == NULL) return;
    for(int i = 0; i < BASE; ++i) clear(root->next[i]);
    free(root);
    return;
}

void insert(Node *root, const char *s) {
    Node *p = root;
    for (int i = 0; s[i]; i++) {
        int idx = s[i] - 'a';
        if (p->next[idx] == NULL) p->next[idx] = getNewNode();
        p = p->next[idx];
    }
    p->flag = 1;
    return ;
}

int find(Node *root, const char *s) {
    Node *p = root;
    for (int i = 0; s[i]; i++) {
        int idx = s[i] - 'a';
        p = p->next[idx];
        if (p == NULL) return 0;
    }
    return p->flag;
}

// k is level number of tree
void output(Node *root, int k, char *buff) {
    buff[k] = 0;
    if (root->flag) {
        printf("trie order: %s\n", buff);
    }
    for (int i = 0; i < BASE; i++) {
        if (root->next[i] == NULL) continue;
        buff[k] = i + 'a';
        output(root->next[i], k + 1, buff);
    }
    return;
}

int main(){
    int op;
    char s[100];
    Node *root = getNewNode();
    do {
        cin >> op;
        if (op == 3) break;
        cin >> s;
        switch (op) {
            case 1:
                printf("insert %s to trie\n", s);
                insert(root, s);
                break;
            case 2:
                printf("find %s from trie : %d\n",s, find(root, s));
                break;
        }
    } while (1);
    output(root, 0, s);
    clear(root);
    return 0;
}